package org.dromara.business.modules.contactForm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.modules.contactForm.domain.bo.BuContactFormBo;
import org.dromara.business.modules.contactForm.domain.vo.BuContactFormVo;
import org.dromara.business.modules.contactForm.service.IBuContactFormService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 联系消息
 *
 * @author ckk
 * @date 2025-11-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/contactForm")
public class BuContactFormController extends BaseController {

    private final IBuContactFormService buContactFormService;

    /**
     * 查询联系消息列表
     */
    @SaCheckPermission("business:contactForm:list")
    @GetMapping("/list")
    public TableDataInfo<BuContactFormVo> list(BuContactFormBo bo, PageQuery pageQuery) {
        return buContactFormService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出联系消息列表
     */
    @SaCheckPermission("business:contactForm:export")
    @Log(title = "联系消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BuContactFormBo bo, HttpServletResponse response) {
        List<BuContactFormVo> list = buContactFormService.queryList(bo);
        ExcelUtil.exportExcel(list, "联系消息", BuContactFormVo.class, response);
    }

    /**
     * 获取联系消息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:contactForm:query")
    @GetMapping("/{id}")
    public R<BuContactFormVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(buContactFormService.queryById(id));
    }

    /**
     * 新增联系消息
     */
    @SaCheckPermission("business:contactForm:add")
    @Log(title = "联系消息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BuContactFormBo bo) {
        return toAjax(buContactFormService.insertByBo(bo));
    }

    /**
     * 删除联系消息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:contactForm:remove")
    @Log(title = "联系消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(buContactFormService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量已读
     */
    @SaCheckPermission("business:contactForm:batchRead")
    @Log(title = "联系消息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/batchRead")
    public R<Void> batchRead(@NotEmpty(message = "主键不能为空") @RequestBody Long[] ids) {
        return toAjax(buContactFormService.batchRead(List.of(ids)));
    }
}
